package com.ruoyi.residents.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.pojo.*;
import com.ruoyi.residents.dto.TbResidentsDTO;
import com.ruoyi.residents.service.*;
import com.ruoyi.residents.vo.TbResidentsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("residents")
public class TbResidentsController {

    @Autowired
    private TbResidentsService tbResidentsService;

    @Autowired
    private TbResidentsFloorService tbResidentsFloorService;

    @Autowired
    private TbBuildingblockService tbBuildingblockService;

    @Autowired
    private TbUnitService tbUnitService;

    @Autowired
    private TbFloorService tbFloorService;

    @Autowired
    private TbRoomService tbRoomService;

    @Autowired
    private TbDocumentTypesService tbDocumentTypesService;

    @Autowired
    private TbResidentsTypesService tbResidentsTypesService;

    @Transactional
    @PostMapping("findPage")
    public Page<TbResidentsDTO> findPage(Integer pageNum,Integer pageSize,@RequestBody TbResidentsVO tbResidentsVO){

        //查询住户信息
        Page<TbResidents> tbResidentsList = tbResidentsService.findList(pageNum,pageSize,tbResidentsVO);

        //拿到住户信息集合
        List<TbResidents> records = tbResidentsList.getRecords();

        //创建返回的集合
        ArrayList<TbResidentsDTO> tbResidentsDTOS = new ArrayList<>();

        //遍历
        for (TbResidents residents : records) {

            //获取返回的对象
            TbResidentsDTO tbResidentsDTO = new TbResidentsDTO();
            //存入数据
            BeanUtils.copyProperties(residents,tbResidentsDTO);

            //当前用户id
            Integer residentsId = residents.getId();

            //当前住户证件类型id查询证件类型表
            Integer documentTypeId = residents.getDocumentTypeId();
            TbDocumentTypes tbDocumentTypes = tbDocumentTypesService.getById(documentTypeId);
            BeanUtils.copyProperties(tbDocumentTypes,tbResidentsDTO);

            //当前住户类型id查询住户类型表
            Integer residentTypeId = residents.getResidentTypeId();
            TbResidentTypes residentTypes = tbResidentsTypesService.getById(residentTypeId);
            BeanUtils.copyProperties(residentTypes,tbResidentsDTO);

            //根据用户id查询中间表
            LambdaQueryWrapper<TbResidentsFloor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TbResidentsFloor::getResidentsId,residentsId);
            TbResidentsFloor tbResidentsFloor = tbResidentsFloorService.getOne(wrapper);

            log.info("tbResidentsFloor------------------------------------------------:"+tbResidentsFloor);

            if (tbResidentsFloor != null) {

                //根据楼栋id获取楼栋信息
                Integer buildingblockId = tbResidentsFloor.getBuildingblockId();
                TbBuildingblock buildingblock = tbBuildingblockService.getById(buildingblockId);
                BeanUtils.copyProperties(buildingblock,tbResidentsDTO);

                //根据单元id获取单元信息
                Integer unitId = tbResidentsFloor.getUnitId();
                TbUnit unit = tbUnitService.getById(unitId);
                BeanUtils.copyProperties(unit,tbResidentsDTO);

                //根据楼层id获取楼层信息
                Integer floorId = tbResidentsFloor.getFloorId();
                TbFloor tbFloor = tbFloorService.getById(floorId);
                BeanUtils.copyProperties(tbFloor,tbResidentsDTO);

                //根据房间id获取房间信息
                Integer roomId = tbResidentsFloor.getRoomId();
                TbRoom tbRoom = tbRoomService.fingdById(roomId);
                BeanUtils.copyProperties(tbRoom,tbResidentsDTO);

            }
            //重新将当前对象的id赋值给返回对象
            tbResidentsDTO.setId(residentsId);

            //将返回的对象存入集合
            tbResidentsDTOS.add(tbResidentsDTO);
        }

        log.info("tbResidentsDTOS:"+tbResidentsDTOS);

        //重新创建返回的page对象
        Page<TbResidentsDTO> tbResidentsDTOPage = new Page<>(pageNum,pageSize);
        //原page参数赋值给新的page对象
        tbResidentsDTOPage.setRecords(tbResidentsDTOS);
        tbResidentsDTOPage.setTotal(tbResidentsList.getTotal());
        tbResidentsDTOPage.setSize(tbResidentsList.getSize());
        tbResidentsDTOPage.setCurrent(tbResidentsList.getCurrent());

        //返回新的page对象
        return tbResidentsDTOPage;

    }

    @PostMapping("updateAppUsageStatus")
    public void updateAppUsageStatus(@RequestBody TbResidents tbResidents){
        tbResidentsService.updateAppUsageStatus(tbResidents);
    }

}
